package top.plgxs.basic.one;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p> 异常 </p>
 *
 * @author Stranger.
 * @since 2022/9/7 11:06
 */
public class ExceptionA {
    public static Integer num = 10;
    public static String str = "abc";

    public static void main(String[] args) {
        ExceptionA a = new ExceptionA();
        // 运行时异常
//        nullPointerException();
//        a.clsscastException();
//        numberFormatException();
//        indexOfBoundsException();
        // 检查型异常
        parseException();
    }

    // NullPointerException——空指针异常
    public static void nullPointerException() {
        ExceptionA exceptionA = null;
        System.out.println(exceptionA.toString());
    }
    // ClassCastException——类转换异常
    public void clsscastException() {
        Animal dog = new Dog();
        Animal cat = new Cat();

        Dog d1 = (Dog) dog;
        Dog d2 = (Dog) cat;  // 这一行报错
    }
    // NumberFormatException——字符串转数字异常
    public static void numberFormatException() {
        Integer a = Integer.valueOf("abc");
        System.out.println(a);
    }
    // ArrayIndexOutOfBoundsException——数组越界异常
    public static void indexOfBoundsException() {
        int[] arr = new int[]{1, 2, 3, 4, 5};
        System.out.println(arr[5]);
    }
    // ParseException
    public static void parseException() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = "abc";

        try {
            Date date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    class Animal {
    }

    class Dog extends Animal{

    }

    class Cat extends Animal {
    }

}
